<template>
  <div>
    <!-- 按钮 -->
    <el-button
      type="primary"
      icon="el-icon-plus"
      style="margin-left: 20px; margin-bottom: 20px"
      @click="addTrademark"
      >添加</el-button
    >
    <!-- 表格 -->
    <el-table
      :data="trademarkList"
      style="width: 100%; margin-bottom: 20px"
      v-loading="loading"
      border
    >
      <el-table-column type="index" label="序号" align="center" width="80">
      </el-table-column>
      <el-table-column prop="tmName" label="品牌名称"> </el-table-column>
      <el-table-column label="品牌logo">
        <template slot-scope="scope">
          <img
            :src="scope.row.logoUrl"
            alt="ddd"
            style="width: 100px; height: 70px"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="{ row }">
          <el-button
            icon="el-icon-edit"
            size="mini"
            type="warning"
            @click="changeTrademark(row)"
            >修改</el-button
          >
          <el-button icon="el-icon-delete" size="mini" type="danger" @click="open(row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[3, 6, 9, 12]"
      :page-size="limit"
      layout="prev, pager,next,jumper,->,sizes,total "
      :total="total"
      style="text-align: center; margin-right: 50px"
    >
    </el-pagination>

    <el-dialog :title="`${form.id?'品牌修改':'品牌添加'}`" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="ruleForm">
        <el-form-item
          label="图片名称"
          :label-width="formLabelWidth"
          prop="tmName"
        >
          <el-input v-model="form.tmName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="图片logo"
          :label-width="formLabelWidth"
          prop="logoUrl"
        >
          <el-upload
            class="avatar-uploader"
            action="/dev-api/admin/product/fileUpload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="form.logoUrl" :src="form.logoUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Trademark",
  data() {
    return {
      page: 1,
      limit: 3,
      total: 0,
      loading: true,
      dialogFormVisible: false,
      form: {
        tmName: "",
        logoUrl: "",
      },
      formLabelWidth: "100px",
      trademarkList: [],
      ruleForm: {
        tmName: "",
        logoUrl: "",
      },
       rules: {
          tmName: [
            { required: true, message: '请输入活动名称', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
          logoUrl: [
            { required: true, message: '请选择活动区域', trigger: 'change' }
          ],
       }
    };
  },
  mounted() {
    this.handeler();
  },
  methods: {
     open(trademark) {
        this.$confirm(`是否删除${trademark.tmName}?`, '确认信息', {
          distinguishCancelAndClose: true,
          confirmButtonText: '确认删除',
          cancelButtonText: '放弃修改'
        })
          .then(async () => {
          let res = await this.$api.trademark.deleteBaseTrademark(trademark.id)
            if(res.code == 200){
              this.$message.success('成功')
              this.handeler(this.trademarkList.length===1?this.page-1:this.page)
            }else{
              this.$message.error('失败')
            }     
          })
          .catch(() => {
          });
      },
     submitForm() {
        this.$refs['ruleForm'].validate( async (valid) => {
          console.log(valid)
          if (!valid) {
            return false
          }
            let result =await this.$api.trademark.addOrUpdateTrademark(this.form)
            if(result.code !=200){
                this.$message.error('操作失败')
                return
            }
            this.$message.success('操作成功')
            this.dialogFormVisible= false
            this.handeler(this.form.id?this.page:1)
          }
        );
      },
    async handeler() {
      this.loading = true;
      let result = await this.$api.trademark.categoryPage(
        this.page,
        this.limit
      );
      if (result.code == 200) {
        this.trademarkList = result.data.records;
        this.total = result.data.total;
        this.loading = false;
        // console.log((this.trademarkList = result.data.records));
      }
    },
    handleSizeChange(val) {
      this.limit = val;
      this.handeler();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.handeler();
    },
    handleAvatarSuccess(res, file) {
      this.form.logoUrl = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = ["image/jpeg", "image/png"].includes(file.type);

      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    addTrademark() {
      this.form = {
        tmName: "",
        logoUrl: "",
      };
      this.dialogFormVisible = true;
      this.$nextTick(()=>{
        this.$refs.ruleForm.clearValidate()
      })
    },
    changeTrademark(row) {
      
      this.form = {...row}
      this.dialogFormVisible = true;
    },
  },
};
</script>

<style >
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>